package com.qiye.livevideo.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.qiye.livevideo.common.RetConstant;
import com.qiye.livevideo.model.AliCloudMessageInfo;
import com.qiye.livevideo.service.AliCloudMessageInfoService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created on 2019/5/1.
 *
 * @author meishijun
 */
@Component
public class AliMessageUtils  {
    /**
     * 产品名称:云通信短信API产品,开发者无需替换
     */
    private static final String  PRODUCT= "Dysmsapi";
    /**
     * 产品域名,开发者无需替换
     */
    private static final String  DOMAIN= "dysmsapi.aliyuncs.com";

    @Autowired
    private  AliCloudMessageInfoService aliCloudMessageInfoService;
    private static AliMessageUtils aliMessageUtils;

    @PostConstruct
    public void init() {
        aliMessageUtils = this;
        aliMessageUtils.aliCloudMessageInfoService = this.aliCloudMessageInfoService;

    }
    public static AliCloudMessageInfo getMessageInfoByAccessKeyId(String accessKeyId){
        if(accessKeyId==null || accessKeyId.isEmpty()){
            accessKeyId = RetConstant.DEFAULT_ACCESS_KEY_ID;
        }
        AliCloudMessageInfo aliCloudMessageInfo = aliMessageUtils.aliCloudMessageInfoService.selectByaccessKeyId(accessKeyId);
        if(aliCloudMessageInfo!=null){
            return aliCloudMessageInfo;
        }
        return new AliCloudMessageInfo();
    }

    public static  IAcsClient  getClient(AliCloudMessageInfo aliCloudMessageInfo){
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        // 初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", aliCloudMessageInfo.getAccessKeyId(), aliCloudMessageInfo.getAccessKeySeret());
        try {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", PRODUCT, DOMAIN);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new DefaultAcsClient(profile);
    }

    /**
     * 发送短信方法
     * @param phone
     * @param code
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSmsByTemplate(String phone, String code,String accessKeyId) throws ClientException {
        AliCloudMessageInfo aliCloudMessageInfo = getMessageInfoByAccessKeyId(accessKeyId);
        IAcsClient acsClient = getClient(aliCloudMessageInfo);
        // 组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        // 必填:待发送手机号
        request.setPhoneNumbers(phone);
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName(aliCloudMessageInfo.getSignName());
        // 必填:短信模板-可在短信控制台中找到
        String jsonParam = "";
        if (aliCloudMessageInfo.getTempleteCode() != null && !aliCloudMessageInfo.getTempleteCode().isEmpty()) {
            request.setTemplateCode(aliCloudMessageInfo.getTempleteCode());
            // 可选:模板中的变量替换JSON串,如模板内容为"尊敬的用户,您的验证码为${code}"时,此处的值为
            jsonParam = "{code:" + code + "}";
            request.setTemplateParam(jsonParam);
        }
        // hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }
}
